package com.gxg.kotlin.mydemo10.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.bigkoo.convenientbanner.ConvenientBanner
import com.google.gson.Gson
import com.gxg.kotlin.mydemo10.R
import com.gxg.kotlin.mydemo10.adapter.HomeAdapter
import com.gxg.kotlin.mydemo10.bean.HomeHeadBean
import com.gxg.kotlin.mydemo10.bean.TopBean
import com.gxg.kotlin.mydemo10.http.API
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_home.*
import org.json.JSONObject
import java.util.*
import kotlin.collections.ArrayList

class HomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        initView();
        initData();
        initHeadData();
        initListener();
    }


     var imgs:ArrayList<String> = ArrayList<String>();
    private fun initHeadData() {
        //创建空的map集合
        val map = HashMap<String, Any>()
        map.put("start", "1")
        map.put("count", "3")
        var json = JSONObject(map);
        OkGo.post<String>(API.String.THEATERS_HOTS).tag(this).upJson(json).execute(object :StringCallback(){
            override fun onSuccess(response: Response<String>) {
                Log.e("sss", "initHeadData: "+response.body().toString())
                var fromJson = Gson().fromJson(response.body().toString(), HomeHeadBean::class.java)
                for(subjectsBean in fromJson.subjects!!){
                    imgs.add(subjectsBean.images?.small!!);
                }
                Log.e("sss", "initHeadData: "+imgs.toString())


            }
        })
    }

    private fun initListener() {
        refresh.setOnRefreshListener {
            refresh.postDelayed(Runnable {
                refresh.finishRefresh();
            },100)
        }
    }

    private fun initData() {
        //创建空的map集合
        val map = HashMap<String, Any>()
        map.put("start", "1")
        map.put("count", "20")
        var json = JSONObject(map);
        OkGo.post<String>(API.String.TOP250).tag(this).upJson(json).execute(object : StringCallback() {
            override fun onSuccess(response: Response<String>) {
                Log.e("sss", "onCreate: "+response.body().toString())
                val fromJson = Gson().fromJson(response.body().toString(), TopBean::class.java)
                Log.e("sss ",fromJson.title)
                homeAdapter.setNewData(fromJson.subjects)
            }
        })

    }

    lateinit  var   homeAdapter:HomeAdapter;
    private fun initView() {
        homeAdapter = HomeAdapter(java.util.ArrayList<TopBean.SubjectsBean>());
        rv.layoutManager = LinearLayoutManager(this);
        homeAdapter.addHeaderView(headView)
        rv.adapter = homeAdapter;

    }
    private var banner: ConvenientBanner<*>? = null
    val  headView: View
        get() {
            val view = View.inflate(this, R.layout.home_head, null)
            banner = view.findViewById(R.id.banner) as ConvenientBanner<*>
            return view;
        }


}
